import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // 一级路由
    // {
    //   path: '/',
    //   redirect: '/home'
    // },
    {
      path: '/',
      component: () => import('@/views/login/LoginPage.vue')
    },
    {
      path: '/home',
      component: () => import('@/views/Amd/HomePage.vue'),
      redirect: '/home/user/index',
      children: [
        {
          path: '/home/user/index',
          component: () => import('@/views/IndexPage/IndexPage.vue')
        },
        {
          path: '/home/user/Aipage',
          component: () => import('@/views/AiPage/AiPage.vue')
        },
        {
          path: '/home/user/SerachPage',
          component: () => import('@/views/SearchPage/SearchPage.vue')
        },
        {
          path: '/home/user/UserDetail',
          component: () => import('@/views/UserDetail/UserDetail.vue')
        },
        {
          path: '/home/user/QuestionDetail',
          component: () =>
            import('@/views/QuestionDetailPage/QuestionDetailPage.vue')
        },
        {
          path: '/home/user/UserQuestionDetail',
          component: () =>
            import('@/views/UserQuestionDetailPage/UserQuestionDetailPage.vue')
        },
        {
          path: '/home/user/MyQuestion',
          component: () => import('@/views/MyQuestionPage/MyQuestionPage.vue')
        },
        {
          path: '/home/user/MyAnswer',
          component: () => import('@/views/MyAnswerPage/MyAnswerPage.vue')
        },
        {
          path: '/home/user/MyReportQuestion',
          component: () =>
            import('@/views/MyReportQuestionPage/MyReportQuestionPage.vue')
        },
        {
          path: '/home/user/MyReportAnswer',
          component: () =>
            import('@/views/MyReportAnswerPage/MyReportAnswerPage.vue')
        },
        {
          path: '/home/user/MyCollect',
          component: () =>
            import('@/views/MyCollectQuestionPage/MyCollectQuestionPage.vue')
        }
      ]
    }
  ]
})
import { useUserStore } from '@/stores'
router.beforeEach((to) => {
  // 如果没有token, 且访问的是非登录页，拦截到登录，其他情况正常放行
  const useStore = useUserStore()
  if (!useStore.token && to.path !== '/') return
  // '/'
})

export default router
